package com.tfswx.jkgl.core.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author Ricky
 * @date 2020/12/23
 */
@Slf4j
@Data
@Component
@PropertySource(value = {"classpath:git.properties"}, ignoreResourceNotFound = true)
@ConfigurationProperties(prefix = "git")
public class GitConfig {
    private Build build;
    private Commit commit;

    public String getVersion() {
        if (build != null) {
            return build.version;
        }
        return "";
    }

    @PostConstruct
    void init() {
        if (build != null && commit != null) {
            log.info("当前版本：{}，当前ID：{}，版本提交时间：{}",
                    build.version,
                    commit.id,
                    build.getTime());
        }
    }

    @Data
    public static class Build {
        private String time;
        private String version;

        public String getTime() {
            return time.replace("T", " ").replace("+0800", "");
        }
    }

    @Data
    public static class Commit {
        private String id;
    }
}
